﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Common.CommandTrees;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using CQIE.TES.IDAL;
using CQIE.TES.Model;
using System.Data.Entity.Validation;
using System.Data.Entity.Core.Objects;

namespace CQIE.TES.DAL
{
    public class TeacherDAL : DALBase<Teacher>, ITeacherDAL
    {
        public void test()
        {
            using (MyDB db = new MyDB())
            {
                var list = db.Teacher.ToList();
                foreach (Teacher teacher in list)
                {
                    string role = teacher.Role1.Name;
                    string Name = teacher.Name;
                }
            }
        }

        public Teacher FindById(int id)
        {
            using (MyDB db = new MyDB())
            {
                return db.Teacher.Include(r => r.Teacher_Course).FirstOrDefault(r => r.ID == id);
            }
        }

        public void testdel()
        {
            using (MyDB db = new MyDB())
            {
                var model = db.Teacher.First(r => r.Name.Contains("Ivan"));
                db.Teacher.Remove(model);
                db.SaveChanges();
            }
        }

        public List<Teacher> GetTeachersPage(int pageNum, int pageSize, string search, out int count)
        {
            using (MyDB db = new MyDB())
            {
                count = db.Teacher.Where(tea => tea.Name.Contains(search)).Count();
                // 模糊查询 Contains()
                // Skip():是跳过前n个结果，返回剩余的结果
                // Take():作用是从查询结果中提取前n个结果
                var query = (from t in db.Teacher
                             where t.Name.Contains(search)
                             orderby t.ID
                             select t).Skip(pageNum * pageSize).Take(pageSize).ToList();

                return query.Select(r => new Teacher()
                {
                    ID = r.ID,
                    Name = r.Name,
                    sex = r.sex,
                    WorkNumber = r.WorkNumber,
                    Birthday = r.Birthday,
                    Phone = r.Phone,
                    Info = r.Info,
                    RoleName = r.Role1.Name,
                    IsFreeze = r.IsFreeze
                }).ToList();
            }
        }

        public bool FreezeUser(int id)
        {
            using (MyDB db = new MyDB())
            {
                try
                {
                    Teacher model = db.Teacher.Find(id);
                    model.IsFreeze = !model.IsFreeze;
                    db.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }

            }
        }

        /// <summary>
        /// 重置密码为123456
        /// </summary>
        /// <param name="id"></param>
        public void ResetPassWord(int id)
        {
            using (MyDB db = new MyDB())
            {
                var query = (from tea in db.Teacher
                             where tea.ID == id
                             select tea).ToArray();
                query[0].PassWord = "123456";
                db.SaveChanges();

            }
        }

        /// <summary>
        /// 获取所有教师
        /// </summary>
        /// <returns></returns>
        public List<Teacher> GetAllTeachers()
        {
            using (MyDB db = new MyDB())
            {
                var list = (from t in db.Teacher
                            where t.IsFreeze==false
                            select t).ToList();

                return list;
            }
        }

        /// <summary>
        /// 添加插入
        /// </summary>
        /// <param name="teacherName"></param>
        /// <param name="age"></param>
        /// <param name="workNumber"></param>
        /// <param name="phone"></param>
        /// <param name="role"></param>
        /// <param name="info"></param>
        public void InsertTeacher(int sex,string teacherName, DateTime age, string workNumber, string phone, int role, string info)
        {
            using (MyDB db = new MyDB())
            {
                // 写数据库
               
                db.Teacher.Add(new Teacher()
                {
                    Name = teacherName,
                    Birthday = age,
                    WorkNumber = workNumber,
                    Phone = phone,
                    Role = role,
                    Info = info,
                    IsFreeze = false,
                    sex = sex,
                    PassWord="123456"
                });

                
                
                db.SaveChanges();

            }
        }

        public void Updata(int id, int sex, string teacherName,string workNumber, string phone, int role, string info)
        {
            using (MyDB db = new MyDB()) 
            {
                if (id > 0)
                {
                    var query = (from tea in db.Teacher
                                 where tea.ID == id
                                 select tea).ToArray();
                    query[0].Name = teacherName;
                    //query[0].Birthday = age;
                    query[0].WorkNumber = workNumber;
                    query[0].Phone = phone;
                    query[0].Role = role;
                    query[0].Info = info;
                    query[0].sex = sex;
                }
                db.SaveChanges();
            }    
        }

        public List<Teacher> GetDepartMentTeachers(int uid)
        {
            using (MyDB db = new MyDB())
            {
                var teacher = db.Teacher.Find(uid);
                var list = teacher.Department.Teachers.ToList();
                list.Remove(teacher);
                return list;
            }
        }

        public Teacher Login(string username, string password)
        {
            using (MyDB db = new MyDB())
            {
                var model = db.Teacher.FirstOrDefault(r => r.WorkNumber == username && r.PassWord == password);
                return model;
            }
        }

        public List<Teacher> SearchByName(string name)
        {
            using (MyDB db = new MyDB())
            {
                return db.Teacher.Where(r => r.IsFreeze==false && r.Name.Contains(name)).ToList();
            }
        }
    }
}
